package mutations;

import java.util.ArrayList;
import java.util.List;

import neural.Individual;

import core.Problem;

public class NoUniformGeneration implements Mutation {

	private double probability;
	private int generationsNumber;
	private double c;

	/*
	 * cada N generaciones pmut = pmut * C donde 0 < C < 1
	 */
	public NoUniformGeneration(double probability, double c,
			int generationsNumber) {
		this.probability = probability;
		this.generationsNumber = generationsNumber;
		this.c = c;
	}

	public Individual conventional(Individual individual) {
		for (int i = 0; i < individual.getWeights().size(); i++) {
			if (Math.random() < probability) {
				//
				int sign = - 1;
				if(Math.random() < 0.5)
					sign = 1;
				individual.getWeights().set(i, sign * Math.random() * 1.5);
			}
		}
		return individual;
	}
	
	public List<Individual> conventionalAll(List<Individual> childs){
		List<Individual> result = new ArrayList<Individual>();
		for(Individual value:  childs){
			result.add(conventional(value));
		}
		return result;
	}

	@Override
	public List<Individual> mutate(List<Individual> childs, Problem problem){
		if (problem.getGenerations() != 0
				&& problem.getGenerations() % generationsNumber == 0) {
			probability = c * probability;
		}

		return conventionalAll(childs);
	}

}
